package com.bioscript.standalone.imps;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;

import com.bioscript.standalone.IO.FileReader;
import com.bioscript.standalone.dataStructures.Generation;
import com.bioscript.standalone.dataStructures.Project;
import com.bioscript.standalone.dataStructures.Range;

public class SNPVisulalizingScript extends Visualiser {
	/**
	 * @param args
	 * @throws ParserConfigurationException
	 */
	public static void main(String[] args) throws ParserConfigurationException
	{
		SNPVisulalizingScript snpVisulalizingScript = new SNPVisulalizingScript();
		List<List<Range>> list = new ArrayList<List<Range>>();
		int seqLength = Integer.parseInt(args[0]);
		String projectFolder = args[1];
		String vxmlFile = args[2];
		String title = args[3];
		String projectDescriptionFile = args[4];
		String snpFileName = args[5];
		
		ProjectDescription description = (new ProjectDescription()).parse(projectDescriptionFile);
		Project prj = description.getLst().get(0);
		
		List<Generation> gens = prj.getGenerations();
		
		String[] names = new String[gens.size()];
		
		int index = 0;
		for (Generation gen : gens)
		{
			list.add(FileReader.readProcessedSNPs(projectFolder + "/" + gen.getName() + "/" + snpFileName));
			names[index] = gen.getName();
			index++;
		}
		snpVisulalizingScript.makeVisualisation(list, seqLength, names, vxmlFile,title);
	}
	
	public void makeVisualisation(List<List<Range>> ranges,int seqLength,String[] names, 
			String vXml, String title) throws ParserConfigurationException
	{
		initialise(String.valueOf(seqLength),names, title);
		makeFeatureSlots(names.length);
		int index = 0;
		for(List<Range> range : ranges)
		{
			for(Range r : range)
			{
				if (r.getLowerLimit() < 0)
				{
					String mouseOver = "Start = " + String.valueOf(seqLength + r.getLowerLimit())
					+ ", stop = " + String.valueOf(seqLength);
					Element feature = prepareXMLFile.createFeature("directStrand" + index, colors[index], 
							"clockwise-arrow", 
							r.getName(), null,mouseOver,null,null,null);
					prepareXMLFile.createFeatureRange(feature, String.valueOf(seqLength + r.getLowerLimit()), 
							String.valueOf(seqLength), null);
					
					mouseOver = "Start = " + String.valueOf(1)
					+ ", stop = " + String.valueOf(r.getUpperLimit());
					feature = prepareXMLFile.createFeature("directStrand" + index, colors[index], 
							"clockwise-arrow", 
							r.getName(), null,mouseOver,null,null,null);
					prepareXMLFile.createFeatureRange(feature, String.valueOf(1), 
							String.valueOf(r.getUpperLimit()), null);
				}
				else
				{
					String mouseOver = "Start = " + String.valueOf(r.getLowerLimit())
					+ ", stop = " + String.valueOf(r.getUpperLimit());
					Element feature = prepareXMLFile.createFeature("directStrand" + index, colors[index], 
							"clockwise-arrow", 
							r.getName(), null,mouseOver,null,null,null);
					prepareXMLFile.createFeatureRange(feature, String.valueOf(r.getLowerLimit()), 
							String.valueOf(r.getUpperLimit()), null);
				}
			}
			index++;
		}
		prepareXMLFile.writeToFile(vXml);
	}
}
